﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QExcel
{
    public class QRows
    {
        public QTable Table
        {
            get;
            set;
        }

        public int Count
        {
            get
            {
                count = 0;
                this.GetRowsCount(this.rows);
                return count;
            }
        }

        List<QBaseRow> leafRows = new List<QBaseRow>();
        /// <summary>
        /// 获取最底层的叶子节点
        /// </summary>
        public List<QBaseRow> AllLeafRows
        {
            get
            {
                leafRows.Clear();
                this.GetLeafColumns(this.rows);
                return leafRows;
            }
        }

        private void GetLeafColumns(List<QBaseRow> list)
        {
            foreach (QBaseRow row in list)
            {
                if (row.LeafRows.Count == 0)
                {
                    leafRows.Add(row);
                }
                else
                {
                    this.GetLeafColumns(row.LeafRows);
                }
            }
        }

        int count = 0;
        private void GetRowsCount(List<QBaseRow> rows)
        {
            foreach (QBaseRow row in rows)
            {
                if (row.LeafRows.Count == 0)
                {
                    count++;
                }
                else
                {
                    this.GetRowsCount(row.LeafRows);
                }
            }
        }

        public void AddRow(QBaseRow row)
        {
            row.SetCurIndex(this.rows.Count + 1);
            row.Table = this.Table;
            this.rows.Add(row);
        }
        
        private List<QBaseRow> rows = new List<QBaseRow>();
        public List<QBaseRow> Rows
        {
            get
            {
                return rows;
            }
        }
    }
}
